package com.example.guideapp.fragment;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.example.guideapp.R;
import com.example.guideapp.activity.ChooseMsgActivity;
import com.example.guideapp.adapter.MyWishTypeGridViewAdapter;
import com.example.guideapp.bean.WishType;
import com.example.guideapp.utils.DatabaseManage;
import com.example.guideapp.utils.MySqliteDatabaseHelper;

import java.util.List;

/**
 * Created by Administrator on 2017/10/16.
 */

public class Fragment2 extends Fragment {

    public static final String WISHTYPE= "wishType";
    public static final String WISHTYPEID = "wishTypeId";
    public static final String WISHTYPENAME = "wishTypeName";

    private GridView mGridView;
    private LayoutInflater mInFlater;

    private List<WishType> list;

    private MySqliteDatabaseHelper helper;

    private MyWishTypeGridViewAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment2,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        helper = DatabaseManage.getInstance(getActivity());
        mInFlater = LayoutInflater.from(getActivity());

        if (view == null){
            view = mInFlater.inflate(R.layout.fragment2,null);
        }

        mGridView = (GridView) view.findViewById(R.id.fragment2_gridView);


        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from Wish_type",null);
        list = DatabaseManage.wishTypeCursorToList(cursor);

        adapter = new MyWishTypeGridViewAdapter(getActivity(),list);

        mGridView.setAdapter(adapter);

        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(getActivity(), ChooseMsgActivity.class);
                intent.putExtra(Fragment1.TYPE,Fragment2.WISHTYPE);
                intent.putExtra(Fragment2.WISHTYPEID,list.get(position).getId());
                intent.putExtra(Fragment2.WISHTYPENAME,list.get(position).getWishName());
                startActivity(intent);
            }
        });

    }
}
